<template>
  <el-drawer
    v-model="showDrawer"
    :title="title"
    :size="size"
    :close-on-click-modal="false"
    :destroy-on-close="destroyOnClose"
    size="45%"
  >
    <div class="formDrawer">
      <div class="body">
        <slot></slot>
      </div>
      <div class="actions">
        <el-button
          round
          :loading="loading"
          type="primary"
          @click="submit"
          >{{ confirmText }}</el-button
        >
        <el-button
          round
          :loading="loading"
          type="default"
          @click="close"
          >取消</el-button
        >
      </div>
    </div>
  </el-drawer>
</template>

<script setup>
import { ref } from "vue";

const showDrawer = ref(false);

const loading = ref(false);
const showLoading = () => (loading.value = true);
const hideLoading = () => (loading.value = false);

const props = defineProps({
  title: String,
  size: {
    type: String,
    default: "45%",
  },
  // destroy-on-close 控制是否在关闭 Drawer 之后将子元素全部销毁
  // 不支持横杠写法，，所以这里写成驼峰的形式
  destroyOnClose: {
    type: Boolean,
    default: false,
  },
  confirmText: {
    type: String,
    default: "提交",
  },
});

// 打开侧边栏
const open = () => (showDrawer.value = true);

// 关闭侧边栏
const close = () => (showDrawer.value = false);

// 提交
const emit = defineEmits(["submit"]);
const submit = () => emit("submit");

// 向父组件暴露以下方法
defineExpose({
  // 是setup内置的方法，所以不需要引入
  open,
  close,
  showLoading,
  hideLoading,
});
</script>

<style lang="less">
.formDrawer {
  width: 100%;
  height: 100%;
  position: relative;
  @apply flex flex-col;
  .body {
    flex: 1;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 50px;
    overflow-y: auto;
  }
  .actions {
    height: 50px;
    @apply mt-auto flex items-center;
  }
}
</style>
